package com.atguigu.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.elasticsearch.client.RestClient;

@Configuration
public class ElasticSerchConfig {

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        // 1.构建底层RestClient
        RestClient restClient = RestClient.builder(
                new HttpHost("192.168.200.130", 9200)).build();

        // 2.用Jackson序列化器创建Transport
        ElasticsearchTransport transport = new RestClientTransport(
                restClient, new JacksonJsonpMapper());

        // 3.组合Transport生成ElasticsearchClient实例
        ElasticsearchClient client = new ElasticsearchClient(transport);
        //  返回对象
        return client;
    }
}
